package pl.edu.agh.sit.servlets;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pl.edu.agh.sit.logic.Preference;
import pl.edu.agh.sit.logic.WidgetDAO;

import com.google.appengine.repackaged.org.json.JSONObject;

@SuppressWarnings("serial")
public class UsersServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		Map<String,Object> map = new LinkedHashMap<String, Object>();
		
		String[] paramValues = req.getParameterValues("action");
		String action = "add";
		if(paramValues[0]!=null){
			action=paramValues[0];
		}else{
			log("bad action param len");
		}
		
		paramValues = req.getParameterValues("widgetId");
		String widgetId= paramValues[0];
		
		if("add".equals(action)){
			
			paramValues = req.getParameterValues("profileName");
			String preferenceName= paramValues[0];
			
//			paramValues = req.getParameterValues("isProtected");
//			Boolean isProtected=Boolean.parseBoolean(paramValues[0]);
//			
//			paramValues = req.getParameterValues("pass");
//			int pass=0;
//			if(paramValues[0]!=null){
//				pass = Integer.parseInt(paramValues[0]);
//			}
			
			WidgetDAO.addPreference(new Preference(widgetId, preferenceName, 50L, 50L, 50L));
			map.put("status", "ok");
		}
		if("delete".equals(action)){
			paramValues = req.getParameterValues("profileName");
			String profileName= paramValues[0];
			WidgetDAO.delete(widgetId, profileName);
			map.put("status", "ok");
		}
		
		if("all".equals(action)){
			Collection<Preference> preferences = WidgetDAO.getPreferences(widgetId);
			
			map.put("status", "ok");
			map.put("preferences", preferences);
			
		}
		
		if("setParameters".equals(action)){
		 //TODO	
		}
		
		
		JSONObject json = new JSONObject(map);
		
		resp.setContentType("text/plain");
		resp.getWriter().println(json);
	}
}

